import { RouteObject, RouteOption } from "../routes/interface";

/**
 * @description 递归查询对应的路由
 * @param {String} path 当前访问地址
 * @param {Array} routes 路由列表
 * @returns array
 */
export const searchRoute = (
    path: string,
    routes: RouteObject[] = [],
): RouteObject => {
    let result: RouteObject = { path: "" };
    for (let item of routes) {
        if (item.path === path) return item;
        if (item.children) {
            const res = searchRoute(path, item.children);
            if (Object.keys(res).length) result = res;
        }
    }
    return result;
};
/**
 * @description 获取需要展开的submenu
 * @param path
 */
export const getOpenKeys = (path: string) => {
    let newStr: string = "";
    let newArr: any[] = [];
    let arr = path.split("/").map((i) => "/" + i);
    for (let i = 1; i < arr.length - 1; i++) {
        newStr += arr[i];
        newArr.push(newStr);
    }
    return newArr;
};
/**
 * @description 递归当前的路由,找出所有相关联的,生成面包屑导航
 * @param {String} path 当前访问的地址
 * @param {Array} menuList 菜单列表
 * @returns array
 */
export const getBreadcrumbList = (
    path: string,
    menuList: IMenu.MenuOptions[],
) => {
    const tempPath: any[] = [];
    try {
        const getNodePath = (node: IMenu.MenuOptions) => {
            tempPath.push(node);
            if (node.path === path) {
                throw new Error("GOT IT");
            }
            if (node.children && node.children.length > 0) {
                for (let i = 0; i < node.children.length; i++) {
                    getNodePath(node.children[i]);
                }
            }
            tempPath.pop();
        };
        for (let i = 0; i < menuList.length; i++) {
            getNodePath(menuList[i]);
        }
    } catch (err) {
        return tempPath.map((item) => ({ title: item.title }));
    }
};

export const findAllBreadcrumb = (
    menuList: IMenu.MenuOptions[],
): { [key: string]: any } => {
    const handleBreadcrumbList: any = {};
    const loop = (menuItems: IMenu.MenuOptions) => {
        if (menuItems?.children?.length)
            menuItems.children.forEach((item) => loop(item));
        else {
            if (menuItems.path != null) {
                handleBreadcrumbList[menuItems.path] = getBreadcrumbList(
                    menuItems.path,
                    menuList,
                );
            }
        }
    };
    menuList.forEach((item) => loop(item));
    return handleBreadcrumbList;
};

export const loopGenerateMenu = (
    routeArray: RouteObject[],
    arr: IMenu.MenuOptions[] = [],
) => {
    routeArray.forEach((route) => {
        const routeOption: RouteOption = {
            title: route.meta!.title || "默认",
            path: route.path as string,
            meta: route.meta as IMenu.MetaProps,
        };
        if (route.children?.length) {
            let newArr = loopGenerateMenu(route.children);
            routeOption.children = newArr;
            if (!route.meta?.isMenu) {
                arr = arr.concat(newArr);
            }
        }
        route.meta?.isMenu && arr.push(routeOption);
    });
    return arr;
};

/**
 *
 */
export const setLocalStorage = <T>(name: string, params: T) => {
    localStorage.setItem(name, JSON.stringify(params));
};
export const getLocalstorage = <T>(name: string): T => {
    return localStorage.getItem(name)
        ? JSON.parse(localStorage.getItem(name) as string)
        : {};
};
export const removeLocalstorage = (name: string) => {
    localStorage.removeItem(name);
};
